using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using NAVA.MTSLibrary;
using WeifenLuo.WinFormsUI.Docking;

namespace OpenMTS
{
    public partial class MainForm : Form
    {
        private ProjectExplorer projectExplorer = null;
        private EvaluatorWindow evalWindow = null;
        private ProjectFile project = null;
        protected MruStripMenuInline mruMenu;
        static string mruRegKey = "SOFTWARE\\NAVA\\OpenMTS";

        public MainForm()
        {
            InitializeComponent();
            mruMenu = new MruStripMenuInline(fileToolStripMenuItem, recentProjectsToolStripMenuItem, new MruStripMenu.ClickedHandler(OnMruFile), mruRegKey + "\\MRU", 16);
            runToolStripMenuItem.Enabled = false;

            projectExplorer = new ProjectExplorer();
            ProjectFile.ProjectLoadEvent += new ProjectFile.ProjectLoad(projectExplorer.ProjectLoad);

            projectExplorer.Show(dockPanel);

            evalWindow = new EvaluatorWindow();
            ProjectFile.ProjectLoadEvent += new ProjectFile.ProjectLoad(evalWindow.ProjectLoad);
            ProjectFile.ProjectChangedEvent += new ProjectFile.ProjectChanged(evalWindow.ProjectChanged);
            evalWindow.Show(dockPanel,DockState.DockBottom);
        }

        

        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProjectDialog dlg = new NewProjectDialog();
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    project = ProjectFile.createProjectFile(dlg.ProjectName,
                        dlg.ProjectLocation);                    
                    mruMenu.AddFile(project.Location + project.Name);
                    runToolStripMenuItem.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error creating Project");
                }
            }
        }

        private void closeCurrentProject()
        {
            if (project == null) return;
            project = null;
            runToolStripMenuItem.Enabled = false;
            //projectExplorer.
        }

        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = ProjectFile.ProjectFileFilter;
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                project = ProjectFile.loadProjectFile(dlg.FileName);
                projectExplorer.Project = project;                
                mruMenu.AddFile(dlg.FileName);
                runToolStripMenuItem.Enabled = true;
                                
            }
        }
        private void OnMruFile(int number, String filename)
        {
            try
            {
                project = ProjectFile.loadProjectFile(filename);
                projectExplorer.Project = project;
                mruMenu.AddFile(project.Location + project.Name);
                mruMenu.SetFirstFile(number);
                runToolStripMenuItem.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Unable to open project. {0}", ex.Message));
                mruMenu.RemoveFile(number);
            }
            
        }

        public void openDocument(string fullName)
        {
            Document doc = new Document();
            doc.TabText = getFileName(fullName);
            doc.Show(this.dockPanel);
            try
            {
                doc.FileName = fullName;                
            }
            catch (Exception exception)
            {
                doc.Close();
                MessageBox.Show(exception.Message);
            }
        }

        private string getFileName(string fullPath)
        {
            int i = fullPath.LastIndexOf('\\');
            if (i != -1)
            {
                return fullPath.Substring(i + 1);
            }

            return fullPath;

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mruMenu.SaveToRegistry();
            this.Close();
        }

        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Show the Run dialog.
            RunDialog dlg = new RunDialog(this.project);
            DialogResult result = dlg.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    MessageTester mt = null;
                    mt = MessageTester.MessageTesterFactory(project.SchemaLocation);
                    XmlDocument xInputMessage = new XmlDocument();
                    xInputMessage.Load(dlg.InputMessage);

                    XmlDocument xRuleset = new XmlDocument();

                    xRuleset.Load(dlg.Rulesets[0]);
                    string sOutput;

                    sOutput = mt.evaluate(xInputMessage.OuterXml, xRuleset.OuterXml);

                //    viewOutput(sOutput, Settings.Default.ViewAsXml,
                  //      buildFileNameWithPath(Settings.Default.ReportTemplate,
                    //    "ReportLocation"));


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
            
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 a = new AboutBox1();
            a.ShowDialog(this);
        }

        private void viewOutput(string sResultXml, bool bViewAsXml, string sReportPath)
        {
            if (tempFileName.Length > 0)
                File.Delete(tempFileName);

            if (bViewAsXml)
            {
                tempFileName = Path.GetTempFileName();
                tempFileName = tempFileName + ".xml";

                System.IO.StreamWriter sw = System.IO.File.CreateText(tempFileName);
                sw.Write(sResultXml);
                sw.Close();

                webBrowser1.Navigate(tempFileName);

            }
            else
            {

                XmlDocument xsl = new XmlDocument();
                xsl.Load(sReportPath);
                XslCompiledTransform xTransform = new XslCompiledTransform();
                xTransform.Load(xsl.CreateNavigator(), null, null);

                XmlDocument xIn = new XmlDocument();
                xIn.LoadXml(sResultXml);
                MemoryStream ms = new MemoryStream();

                StringBuilder sb = new StringBuilder();
                StringWriter sw1 = new StringWriter(sb);
                xTransform.Transform(xIn.CreateNavigator(), null, sw1);

                tempFileName = Path.GetTempFileName();
                tempFileName = tempFileName + ".htm";

                System.IO.StreamWriter sw = System.IO.File.CreateText(tempFileName);
                sw.Write(sb.ToString());
                sw.Close();

                webBrowser1.Navigate(tempFileName);

            }
        }
       

        

    }
}